
package com.jf.cloud.im.service.impl;

import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.util.PageUtil;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.im.constants.WhoSend;
import com.jf.cloud.im.mapper.ImMsgBizUserMapper;
import com.jf.cloud.im.model.ImMsgBizUser;
import com.jf.cloud.im.model.ImMsgBizUserHistory;
import com.jf.cloud.im.service.ImMsgBizUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.ZoneId;
import java.util.Date;
import java.util.Objects;

/**
 * 用户最近聊天记录表
 *
 * @author zz
 * @date 2021-08-25 13:58:56
 */
@Service
public class ImMsgBizUserServiceImpl implements ImMsgBizUserService {

    @Autowired
    private ImMsgBizUserMapper imMsgBizUserMapper;

    @Override
    public PageVO<ImMsgBizUser> page(PageDTO pageDTO) {
        return PageUtil.doPage(pageDTO, () -> imMsgBizUserMapper.list());
    }

    @Override
    public PageVO<ImMsgBizUser> pageBySendTypeAndBizId(PageDTO pageDTO, Integer sendType, Long bizId) {
        return PageUtil.doPage(pageDTO, () -> imMsgBizUserMapper.listBySendTypeAndBizId(sendType, bizId));
    }

    @Override
    public PageVO<ImMsgBizUser> pageByUserId(PageDTO pageDTO, Long userId) {
        return PageUtil.doPage(pageDTO, () -> imMsgBizUserMapper.listByUserId(userId));
    }

    @Override
    public ImMsgBizUser getById(Long id) {
        return imMsgBizUserMapper.getById(id);
    }

    @Override
    public ImMsgBizUser getBySendTypeBizIdUserId(Integer sendType, Long bizId, Long userId) {
        return imMsgBizUserMapper.getBySendTypeAndBizIdAndUserId(sendType, bizId, userId);
    }

    @Override
    public void save(ImMsgBizUser imMsgBizUser) {
        imMsgBizUserMapper.save(imMsgBizUser);
    }

    @Override
    public void update(ImMsgBizUser imMsgBizUser) {
        imMsgBizUserMapper.update(imMsgBizUser);
    }

    @Override
    public void deleteById(Long id) {
        imMsgBizUserMapper.deleteById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateBizUserLastMsg(ImMsgBizUserHistory imMsgBizUserHistory) {
        ImMsgBizUser imMsgBizUser = this.getBySendTypeBizIdUserId(imMsgBizUserHistory.getSendType(), imMsgBizUserHistory.getBizId(), imMsgBizUserHistory.getUserId());
        if (Objects.nonNull(imMsgBizUser)) {
            imMsgBizUser.setEmployeeId(imMsgBizUserHistory.getEmployeeId());
            if (Objects.equals(imMsgBizUserHistory.getWhoSend(), WhoSend.EMPLOYEE)) {
                imMsgBizUser.setUserUnreadNum(imMsgBizUser.getUserUnreadNum() + 1);
            } else if (Objects.equals(imMsgBizUserHistory.getWhoSend(), WhoSend.USER)) {
                imMsgBizUser.setBizUnreadNum(imMsgBizUser.getBizUnreadNum() + 1);
            }
            imMsgBizUser.setLastMsgId(imMsgBizUserHistory.getId());
            imMsgBizUser.setLastMsgType(imMsgBizUserHistory.getType());
            imMsgBizUser.setLastMsgContent(imMsgBizUserHistory.getContent());
            imMsgBizUser.setLastMsgWhoSend(imMsgBizUserHistory.getWhoSend());
            imMsgBizUser.setLastMsgTime(imMsgBizUserHistory.getCreateTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
            imMsgBizUser.setUpdateTime(new Date());
            this.update(imMsgBizUser);
        }
    }
}
